package com.liang.shortlink.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.shortlink.project.dao.entity.ShortLinkDO;
import com.liang.shortlink.project.dao.mapper.ShortLinkMapper;
import com.liang.shortlink.project.dto.req.RecycleBinPageReqDTO;
import com.liang.shortlink.project.dto.req.RecycleBinRecoverReqDTO;
import com.liang.shortlink.project.dto.req.RecycleBinRemoveReqDTO;
import com.liang.shortlink.project.dto.req.RecycleBinSaveReqDTO;
import com.liang.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import com.liang.shortlink.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.liang.shortlink.project.common.constant.RedisKeyConstant.*;

/**
 * 回收站管理接口实现层
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<ShortLinkMapper, ShortLinkDO> implements RecycleBinService {

    private final StringRedisTemplate redisTemplate;

    @Override
    public void saveRecycleBin(RecycleBinSaveReqDTO requestParam) {

        lambdaUpdate()
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 0)
                .set(ShortLinkDO::getEnableStatus, 1)
                .update();
        redisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<ShortLinkPageRespDTO> pageRecycleBinShortLink(RecycleBinPageReqDTO requestParam) {
        IPage<ShortLinkDO> resultPage = baseMapper.pageRecycleBinLink(requestParam);
        return resultPage.convert(item -> {
            ShortLinkPageRespDTO dto = BeanUtil.toBean(item, ShortLinkPageRespDTO.class);
            dto.setDomain("http://" + item.getDomain());
            return dto;
        });
    }

    @Override
    public void recoverRecycleBin(RecycleBinRecoverReqDTO requestParam) {
        lambdaUpdate()
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getEnableStatus, 1)
                .set(ShortLinkDO::getEnableStatus, 0)
                .update();
        redisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY, requestParam.getFullShortUrl()));
    }

    @Override
    public void removeRecycleBin(RecycleBinRemoveReqDTO requestParam) {
        lambdaUpdate()
                .eq(ShortLinkDO::getGid, requestParam.getGid())
                .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl())
                .eq(ShortLinkDO::getDelFlag, 0)
                .eq(ShortLinkDO::getDelTime, 0L)
                .eq(ShortLinkDO::getEnableStatus, 1)
                .set(ShortLinkDO::getDelFlag, 1)
                .set(ShortLinkDO::getDelTime, System.currentTimeMillis())
                .remove();
        redisTemplate.delete(SHORT_LINK_STATS_UV_KEY + requestParam.getFullShortUrl());
        redisTemplate.delete(SHORT_LINK_STATS_UIP_KEY + requestParam.getFullShortUrl());
        redisTemplate.delete("short-link:goto:nurl.ink:" + requestParam.getFullShortUrl());
    }
}
